package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatGoodsExchange;
import com.happy.cat.project.system.service.ICatGoodsExchangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 商品兑换记录Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/goodsExchange" )
@Api("商品兑换记录 API")
public class CatGoodsExchangeController extends BaseController {

    private final ICatGoodsExchangeService iCatGoodsExchangeService;

    /**
     * 查询商品兑换记录列表
     */
    @ApiOperation(value = "查询商品兑换记录列表",notes = "查询商品兑换记录列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:goodsExchange:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatGoodsExchange catGoodsExchange)
    {
        startPage();
        LambdaQueryWrapper<CatGoodsExchange> lqw = new LambdaQueryWrapper<CatGoodsExchange>();
        if (catGoodsExchange.getGoodsId() != null){
            lqw.eq(CatGoodsExchange::getGoodsId ,catGoodsExchange.getGoodsId());
        }
        if (catGoodsExchange.getUserId() != null){
            lqw.eq(CatGoodsExchange::getUserId ,catGoodsExchange.getUserId());
        }
        List<CatGoodsExchange> list = iCatGoodsExchangeService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出商品兑换记录列表
     */
    @ApiOperation(value = "导出商品兑换记录列表",notes = "导出商品兑换记录列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goodsExchange:export')" )
    @Log(title = "商品兑换记录" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatGoodsExchange catGoodsExchange) {
        LambdaQueryWrapper<CatGoodsExchange> lqw = new LambdaQueryWrapper<CatGoodsExchange>(catGoodsExchange);
        List<CatGoodsExchange> list = iCatGoodsExchangeService.list(lqw);
        ExcelUtil<CatGoodsExchange> util = new ExcelUtil<CatGoodsExchange>(CatGoodsExchange. class);
        return util.exportExcel(list, "goodsExchange" );
    }

    /**
     * 获取商品兑换记录详细信息
     */
    @ApiOperation(value = "获取商品兑换记录详细信息",notes = "获取商品兑换记录详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goodsExchange:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {

        return AjaxResult.success(iCatGoodsExchangeService.getById(id));
    }

    /**
     * 新增商品兑换记录
     */
    @ApiOperation(value = "新增商品兑换记录",notes = "新增商品兑换记录",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goodsExchange:add')" )
    @Log(title = "商品兑换记录" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatGoodsExchange catGoodsExchange) {
        catGoodsExchange.setCreateTime(new Date());
        return toAjax(iCatGoodsExchangeService.save(catGoodsExchange) ? 1 : 0);
    }

    /**
     * 修改商品兑换记录
     */
    @ApiOperation(value = "修改商品兑换记录",notes = "修改商品兑换记录",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goodsExchange:edit')" )
    @Log(title = "商品兑换记录" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatGoodsExchange catGoodsExchange) {
        return toAjax(iCatGoodsExchangeService.updateById(catGoodsExchange) ? 1 : 0);
    }

    /**
     * 删除商品兑换记录
     */
    @ApiOperation(value = "删除商品兑换记录",notes = "删除商品兑换记录",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goodsExchange:remove')" )
    @Log(title = "商品兑换记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatGoodsExchangeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
